﻿/*
This project and all of its code is/are Copyright (C) 2017  Nils Hunter Alving

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UnitTestSTK;
using System.Linq;
using DynamicTypeBuilder.ConstrainedType.Examples;
using Newtonsoft.Json;

namespace DynamicTypeBuilerUnitTests
{
    [TestClass]
    public class ConstrainedTypeUnitTests
    {
        [TestMethod]
        public void RepeatInterval_ToString_OutputsCorrectNameWithSpaces()
        {
            RepeatInterval interval = RepeatInterval.EveryOtherDay;
            Assert.AreEqual("Every other day", interval.ToString());
        }
        [TestMethod]
        public void RepeatInterval_CastToString_OutputsCorrectNameWithSpaces()
        {
            RepeatInterval interval = RepeatInterval.EveryOtherDay;
            Assert.AreEqual("Every other day", (string)interval);
        }
        [TestMethod]
        public void RepeatInterval_CastToInvalidString_ThrowsArgumentException()
        {
            AssertThat.Throws<ArgumentException>(() =>
            {
                RepeatInterval interval = "Once per week";
            });
        }
        [TestMethod]
        public void RepeatInterval_IndexProperty_ReturnsCorrectIndexForAllPredefinedValues()
        {
            var index = 0;
            var repeatIntervals = new RepeatInterval().Values.ToList();

            repeatIntervals.ForEach(v =>
                {
                    var val = new RepeatInterval() { Value = v.Key };
                    Assert.AreEqual(index++, val.Index);
                });
        }
        [TestMethod]
        public void PrimeNumbers_Five_AllowsDirectConversionToAndFromInt()
        {
            PrimeNumbers five = PrimeNumbers.Five;
            int numberFive = five;
            Assert.AreEqual(5, five.Value);
            Assert.AreEqual(numberFive, five.Value);
        }
        [TestMethod]
        public void PrimeNumbersOfTypeInt_IndexProperty_ReturnsCorrectIndex()
        {
            PrimeNumbers eleven = PrimeNumbers.Eleven;
            Assert.AreEqual(4, eleven.Index);
        }
        [TestMethod]
        public void Digits_StandardIndexPattern_ReturnsCorrectIndexForAllValues()
        {
            OneThruFive three = OneThruFive.Three;
            Assert.AreEqual(3, three.Index);
        }
        [TestMethod]
        public void Fives_CustomIndexPattern_ReturnsCorrectIndexForAllValue()
        {
            Fives fifteen = Fives.Fifteen;
            Assert.AreEqual(Fives.Fifteen, fifteen.Index);
        }
        [TestMethod]
        public void InvalidIndexConstrainedType_Constructor_ThrowsException()
        {
            AssertThat.Throws<ArgumentException>(() =>
            {
                InvalidIndexConstrainedType five = InvalidIndexConstrainedType.Five;
            });
        }
        [TestMethod]
        public void RepeatInterval_SerializeToJson_DeserializesCorrectly()
        {
            RepeatInterval interval = RepeatInterval.BiWeekly;
            var json = JsonConvert.SerializeObject(interval);

            RepeatInterval deserializedInterval = JsonConvert.DeserializeObject<RepeatInterval>(json);
            var newJson = JsonConvert.SerializeObject(deserializedInterval);

            Assert.AreEqual(json, newJson);
        }
    }
}
